package com.yao.service.impl;

import com.yao.dao.UserDao;
import com.yao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

//<bean id="userService" class="com.yao.service.impl.UserServiceImpl">
//<property name="userDao" ref="userDao"></property>
@Service("userService")
//@Scope("prototype")
@Scope("singleton")
public class UserServiceImpl implements UserService {

    @Value("${jdbc.driver}")
    private String driver;

//    @Autowired //按照数据类型从Spring容器中进行匹配的
//    @Qualifier("userDao")  //是按照id名称从Spring容器中进行匹配的,但是@Qualifier要结合@Autowireda一起用
    @Resource(name = "userDao") //@Resource相当于@Autowired+@Qualifier
    private UserDao userDao;

    //用注解方式，这个set方法可以不写
//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//    }

    public void save() {
        System.out.println(driver);
        userDao.save();
    }

    @PostConstruct
    public void init(){
        System.out.println("Service对象的初始化方法");
    }

    @PreDestroy
    public void destory(){
        System.out.println("Service对象的销毁方法");
    }
}
